Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
If you think about this for a moment, you can see that events may need to be “passed along” from one object to another. For example, if the user clicks on the Label control in the demonstration ActiveX control—and you want the container object to be able to respond—pass the event “up” to the container. This is accomplished using Visual Basic’s RaiseEvent statement. The syntax is as follows:
    RaiseEvent EventName [(ArgList)]
EventName is the name of the event to fire, and ArgList is an optional list of arguments. Before you can use RaiseEvent, you must declare an event procedure for the event you will raise. This declaration must be at the module level of the module in which the event will be raised; it takes the following form:
    [Public] Event EventName [(ArgList)]
EventName and ArgList are, respectively, the name of the event and an optional argument list. Include the optional Public keyword if the event needs to be detected by another module; otherwise, it will be available only within the module where it is raised. When the event is raised, the argument list used in the RaiseEvent statement must match the list in the event procedure declaration. What events are available? The usual repertoire of Visual Basic events is at your disposal, such as Click and MouseDown. Without RaiseEvent, the control would be able to use events internally, but if you want the container to be able to respond to events, you will have to raise them. Before we write the event code for the FancyCmdButton control, let’s think for a moment about what it needs to do: • When the user presses the mouse button when the pointer is on the control, the control’s background color should change. • When the user releases the mouse button, the background color should change back, and a Click event should be raised, so the container object can respond to it. For the first task, we will use the MouseDown event. But where will this event be detected? The ActiveX control consists of both a Label control and a Shape control, plus the underlying UserControl. Clearly, the Label control must respond to MouseDown. Shape controls do not detect mouse events, so mouse action on our Shape control will be automatically passed through to the underlying UserControl. Thus, the UserControl’s MouseDown event procedure will also be used. In figuring out how to handle the first task, we learn how to do the second, as well. We will use the MouseUp event procedure of the Label control and UserControl. Start by opening the Code window for the FancyCmdButton control. Select General in the Object list and Declarations in the Procedure box, then add the code shown in Listing 17.3. This code declares a variable and a constant for manipulating the control’s color and declares the Click event procedure, so that we can use the RaiseEvent statement. Listing 17.3 Code in the General Declarations section of the ActiveX control module.
    Option Explicit

    ‘ Variable for the old color.
    Dim OldColor As Long

    ‘ Constant for the “clicked” color (this is red).
    Const NEWCOLOR = &HFF&

    ‘ Declare a Public Click event procedure.

    Public Event Click()
The next code must be added to the MouseDown and MouseUp event procedures of the Label and FancyCmdButton. In the Code Editing window, use the Object and Procedure lists to select these procedures, then add the code shown in Listing 17.4. This listing combines the code for the two MouseDown and two MouseUp event procedures. Listing 17.4 The MouseDown and MouseUp event procedures for the FancyCmdButton and the Label control.
    Private Sub lblButton_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    ‘Save the original fill color.
    OldColor = shpButton.FillColor
    ‘ Change to the “clicked” fill color.
    shpButton.FillColor = NEWCOLOR

    End Sub

    Private Sub lblButton_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    ‘ Restore the original fill color.
    shpButton.FillColor = OldColor
    ‘ Raise the click event.
    RaiseEvent Click

    End Sub

    Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    ‘ Save the old fill color.
    OldColor = shpButton.FillColor
    ‘ Change to the “clicked” fill color.
    shpButton.FillColor = NEWCOLOR

    End Sub

    Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    ‘ Restore the original fill color.
    shpButton.FillColor = OldColor

    ‘ Raise a Click event.
    RaiseEvent Click

    End Sub
This completes the code required to have our FancyCmdButton control respond to user clicks by changing its background color and raising a click for its container to respond to. The next task is to add the code to the test project, enabling it to respond to that event. Close the UserControl, and display the Code window for the test project form (TestAXCtl_Form1). Add the single line of code in Listing 17.5 to the form’s Click event procedure. Listing 17.5 The container form’s Click event procedure.
    Private Sub FCB1_Click()

    MsgBox (“I’ve been clicked”)

    End Sub
The project is now ready to take for a spin. Be sure that the FancyCmdButton designer is closed, as indicated by the control on the test form displayed without hatch marks. Also, be sure that the test project is the startup project, as indicated by its name displayed in bold in the Project Explorer window. If it is not, right-click on the project name and select Set As Startup from the pop-up menu. Then, press F5 to run the test project. You’ll see its form displayed, as shown in Figure 17.7. When you position the mouse pointer over the control and press the mouse button, you’ll see the button’s background color change to red. When you release the mouse button, the color changes back to green and a message appears indicating that the form has detected the click. Figure 17.7 Testing the FancyCmdButton control. TIP: ActiveX Testing Options Even if you have created a test project for your ActiveX control, you have the option of testing the control in the Internet Explorer Web browser. If the test project is set as the startup project, the control will run in your test project. If you make the ActiveX control project the startup, then the control will run in Internet Explorer. Before we continue adding to the demonstration control, think for a moment about what happens when you execute the test program and click on the FancyCmdButton control. The MouseDown and MouseUp events are detected by the ActiveX control itself, which responds by changing its background color. In addition, a Click event is raised, and that event is detected by the container—the test program. In response to that event, a message box is displayed. ### Adding Properties To The Control Properties are added to an ActiveX control in the same manner as for any other ActiveX object, as you learned in Chapter 6. To define a property, you create property procedures. The Get procedure makes the property available for reading, and the Let procedure is used to set the property value. To define a read-only property, you can create a Get procedure without a corresponding Let. (Please turn back to Chapters 7 and 8 if you need a refresher on property procedures.) We will create a single property for the FancyCmdButton. It will be called Caption, and it will determine the text that is displayed on the control. Because the value will be stored in the Label control’s Caption property, we do not need to declare a separate variable to hold it.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.